<?php

namespace app\common\command;

use app\common\model\Video;
use samdark\sitemap\Sitemap;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class SiteMapUpdate extends Command
{

    const _BASE_URL_ = 'https://www.xcd.icu';

    protected function configure()
    {
        $this->setName('sitemap_update')
            ->setDescription('更新网站地图');
    }

    public function execute(Input $input, Output $output)
    {
        try {
            $videoModel = new Video();
            $siteMap = new Sitemap(sprintf('%spublic/sitemap.xml', ROOT_PATH));

            $siteMap->addItem(self::_BASE_URL_, time(), Sitemap::DAILY, 1.0);

            $list = $videoModel->field(['id'])->order('view_num', 'desc')->select();

            foreach ($list as $item) {
                $siteMap->addItem(sprintf('%s/index/index/detail/id/%d.html', self::_BASE_URL_, $item['id']), time(), Sitemap::DAILY, 1.0);
            }


            $siteMap->write();

        } catch (\Throwable $exception) {
            $output->error(sprintf("%s[%d]", $exception->getMessage(), $exception->getLine()));
            exit(1);
        }
        $output->info("ok");
    }
}